/**

 @Name：CLTPHP.Pro 内容管理
 @Author：chichu
 @Site：http://www.cltphp.com/
 @License：CLTXY

 */
layui.define(['admin', 'table', 'util','upload','laydate'], function (exports) {
    var $ = layui.$, admin = layui.admin, table = layui.table, form = layui.form, upload = layui.upload,laydate=layui.laydate;
    layui.selMeltiple($);
    //全部数据
    tableIn = table.render({
        elem: '#list',
        url: url,
        method: 'post',
        autoSort: false,
        cols: [[
            {checkbox: true, fixed: true},
            {field: 'id', title: 'ID', width: 80, fixed: true,sort:true},
            {field: 'title', title: '标题', width: 400, templet: '#title'},
            {field: 'hits',  title: '点击', width: 80,sort:true},
            {field: 'createtime', title: '创建时间', width: 180,sort:true},
            {field: 'push_time', title: '发布时间', width: 180,sort:true},
            {field: 'sort', align: 'center', title: '排序', width: 120,edit:'text',sort:true},
            {width: 160, align: 'center', toolbar: '#action',title:'操作'}
        ]],
        page:true
    });
    //监听排序事件
    table.on('sort(list)', function(obj){
        table.reload('list', {
            initSort: obj
            ,where: {sort_by: obj.field,sort_order: obj.type}
        });
    });

    //修改字段
    table.on('edit(list)', function(obj){
        var param={};
        param[obj.field] = obj.value;
        param['id'] = obj.data.id;
        cf(param);
    });
    function cf(param){
        admin.req({
            url: cfUrl,
            data: param,
            type:'post',
            success: function (res) {
                if(res.code==0){
                    layer.msg(res.msg,{time:1000,icon:1});
                }
                tableIn.reload();
            }
        });
    }

    //监听工具条
    table.on('tool(list)', function (obj) {
        var data = obj.data;
        var id = data.id;
        if (obj.event === 'del') {
            layer.confirm('您确定要删除该内容吗？', function () {
                admin.req({
                    url: delUrl
                    , type: 'post'
                    , data: {'ids': id}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                obj.del();
                            });
                        }
                    }
                });
            });
        }else if (obj.event === 'edit') {
            var index = layer.open({
                type: 2,
                content: editUrl+'?id=' + id,
                area: ['300px', '300px'],
                maxmin: true
            });
            layer.full(index);
        }
    });
    //提交表单
    form.on('submit(sub-btn)', function (data) {
        admin.req({
            url: subUrl
            , type: 'post'
            , data: data.field
            , done: function (res) {
                layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                    layer.closeAll("iframe");
                    //刷新父页面
                    parent.location.reload();
                });
            }
        });
    });

    var active = {
        //搜索
        reload: function () {
            var key = $('#key').val();
            //执行重载
            table.reload('list', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                , where: {
                    key: key
                }
            });
        },
        //调用添加页面
        add: function(){
            var index = layer.open({
                type: 2,
                content: addUrl,
                area: ['300px', '300px'],
                maxmin: true
            });
            layer.full(index);
        },
        direct: function () {
            var checkStatus = table.checkStatus('list'), data = checkStatus.data; //获得选中的数据
            if (data.length === 0) return layer.msg('未选中行');
            var ids = [];
            $.each(data, function (i, val) {
                ids[i] = val['id'];
            });
            layer.confirm('确定删除所选内容吗？', function () {
                admin.req({
                    url: delUrl
                    , type: 'post'
                    , data: {ids: ids}
                    , success: function (res) {
                        layer.closeAll('dialog');
                        if (res.code == 0) {
                            layer.msg(res.msg, {icon: 1, time: 1000}, function () {
                                tableIn.reload();
                            });
                        }
                    }
                });
            });
        }
    };
    $('.clt-auth-btns .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    //上传缩略图
    var thumb = $('#thumb');
    upload.render({
        url: '/shengteng/Upfiles/upimage/'
        , elem: '#thumbUpload'
        , done: function (res) {
            if (res.code == 0) {
                thumb.val(res.url);
                thumb.parent().append('<input type="hidden" name="aid[thumb]" class="aid" value="'+res.aid+'">')
            } else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
    //查看缩略图
    admin.events.thumbShow = function (othis) {
        var src = thumb.val();
        layer.photos({
            photos: {
                "title": "查看图片" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };
    //多图删除
    $('.layui-row').on('click','.delimg',function(){
        var thisimg = $(this);
        layer.confirm('你确定要删除该图片吗？', function(index){
            thisimg.parents('.layui-col-md3').remove();
            layer.close(index);
        })
    });

    //缩略图
    admin.events.getAtta = function (a) {
        var imgId = a.data('input');
        var that = this;
        layer.open({
            type: 2
            ,title: '图片资源'
            ,area: ['700px', '690px']
            ,shade: 0
            ,anim: 1
            ,content: '/shengteng/attachment/getList/'
            ,btn: ['选择','取消'] //只是为了演示
            ,yes: function(index){
                var body = layer.getChildFrame('body', index);
                var aid = body.find('.select').data('id');
                var url = body.find('.select').data('url');
                $('#'+imgId).val(url);
                $('#'+imgId).parent().find('.aid').remove();
                $('#'+imgId).parent().append('<input type="hidden" name="aid['+imgId+']" class="aid" value="'+aid+'">')
                layer.close(index);
            }
            ,btn2: function(){
                layer.closeAll();
            }
            ,zIndex: layer.zIndex //重点1
            ,success: function(layero,index){
            }
        });
    };
    //单图
    admin.events.getAttaImg = function (a) {
        var imgId = a.data('input');
        var that = this;
        layer.open({
            type: 2
            ,title: '图片资源'
            ,area: ['700px', '690px']
            ,shade: 0
            ,anim: 1
            ,content: '/shengteng/attachment/getList/'
            ,btn: ['选择','取消'] //只是为了演示
            ,yes: function(index){
                var body = layer.getChildFrame('body', index);
                var aid = body.find('.select').data('id');
                var url = body.find('.select').data('url');
                $('#'+imgId+'Val').val(url);
                $('#'+imgId+'Img').parent().find('.aid').remove();
                $('#'+imgId+'Img').parent().append('<input type=\"hidden\" class=\"aid\" name=\"aid['+imgId+']\" value=\"'+aid+'\">');
                $('#'+imgId+'Img').attr('src',url);
                layer.close(index);
            }
            ,btn2: function(){
                layer.closeAll();
            }
            ,zIndex: layer.zIndex //重点1
            ,success: function(layero,index){
            }
        });
    };
    //多图
    admin.events.getAttaImgs = function (images) {
        var imgId = images.data('input');
        var oldAids = '';
        var box = images.siblings('blockquote').children('#demo'+imgId).children('.layui-row');
        box.find('.oldaid').each(function(){
            oldAids+=$(this).val()+',';
        });
        var that = this;
        layer.open({
            type: 2
            ,title: '图片资源'
            ,area: ['700px', '690px']
            ,shade: 0
            ,anim: 1
            ,content: '/shengteng/attachment/getList.html?type=2&oldAids='+oldAids
            ,btn: ['选择','取消'] //只是为了演示
            ,yes: function(index){
                var body = layer.getChildFrame('body', index);
                var imgHtml='';
                body.find('.select').each(function(){
                    var aid = $(this).data('id');
                    var url = $(this).data('url');
                    imgHtml +='<div class="layui-col-md3">' +
                        '<div class="dtbox"><img src="'+url+'" class="layui-upload-img">' +
                        '<input type="hidden" name="'+imgId+'['+aid+']" class="'+imgId+'" value="'+url+'">' +
                        '<input type="hidden" name="aids['+imgId+'][]" class="oldaid" value="'+aid+'"><i class="delimg layui-icon">&#x1006;</i></div></div>';
                });
                box.append(imgHtml);
                layer.close(index);
            }
            ,btn2: function(){
                layer.closeAll();
            }
            ,zIndex: layer.zIndex //重点1
            ,success: function(layero,index){
            }
        });
    };
    exports('content', {});
});
